package com.ruoyi.Amount.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;

@Mapper
public interface OrderMapper {


    /**
     * 查询当前用户的所属商户
     * @param userId
     */
    @Select("SELECT s.id FROM sys_user u JOIN sys_store s ON u.dept_id = s.dept_id WHERE u.user_id =#{userId}")
    Long getStoreId(Long userId);

    /**
     * 根据商户id查询所属代理商
     * @param storeId
     */
    @Select("SELECT agent_id FROM sys_store WHERE id = #{storeId}")
    Long getAgentId(Long storeId);

    /**
     * 查询商户余额
     * @param storeId
     */
    @Select("SELECT balance FROM sys_store  WHERE id =#{storeId}")
    BigDecimal getStoreBalance(Long storeId);

    /**
     * 查询代理商余额
     * @param agentId
     */
    @Select("SELECT balance FROM sys_agent WHERE id =#{agentId}")
    BigDecimal getAgentBalance(Long agentId);

    /**
     * 根据代理商ID查询所有接口价格之和
     * @param agentId 目标代理商的ID
     * @return 代理商的总价格信息
     */
    @Select("SELECT " +
            "    SUM(identity_price + credit_price + negative_price + commercial_price + education_price + degree_price) AS total_price" +
            " FROM " +
            "    sys_interface_price" +
            " WHERE " +
            "    agent_id = #{agentId}" + // 使用#{agentId}参数化查询，防止SQL注入
            " GROUP BY " +
            "    agent_id")
    BigDecimal getAgentTotalPrice(@Param("agentId") Long agentId);

}
